package com.sc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sc.mapper.RoleMapper;
import com.sc.pojo.Menu;
import com.sc.pojo.Role;
import com.sc.service.RoleSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleSerice {
    @Autowired
    RoleMapper mapper;
    @Override
    public PageInfo<Role> show(Integer pageNum, Integer pageSize,String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> list = mapper.selectAll(keyword);
        return new PageInfo<>(list);
    }

    @Override
    public int add(Role r) {
        return mapper.insertSelective(r);
    }

    @Override
    public int del(Integer id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int upd(Role r) {
        return mapper.updateByPrimaryKeySelective(r);
    }

    @Override
    public int openAll(Integer[] ids) {
        return mapper.openAll(ids);
    }

    @Override
    public int closeAll(Integer[] ids) {
        return mapper.closeAll(ids);
    }

    @Override
    public void saveMenu(Integer[] menuIds, Integer roleid) {
        mapper.delMenusById(roleid);
        for (Integer menuid:menuIds) {
            mapper.insertMenu(roleid,menuid);
        }
    }

    @Override
    public Role query(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int delAll(Integer[] ids) {
        return mapper.delAll(ids);
    }
}
